var tempclassname = "";
// 载入多叉树 
var setting = {
	async : {
		enable : true,
		contentType: "application/x-www-form-urlencoded",
		url : "gxgljson_loadtreenode.action",
		autoParam : [],
		otherParam : {"table":"b_base_cx"}
	},
	edit : {
		enable : false 
	},
	view : {
		// zTree 节点展开、折叠时的动画速度，设置方法同 JQuery 动画效果中 speed 参数
		expandSpeed : ""
	},
	check : {
		enable : false,
		chkStyle : "checkbox"
	},
	data : {
		simpleData : {
			enable : true
		}
	},
	callback : { 
		beforeRename : beforeRename, 
		onDblClick : onDblClick,  
		onAsyncSuccess : onAsyncSuccess
	}
};
// 完成异步加载后 
function onAsyncSuccess(event, treeId, treeNode, msg) {
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo"); 
	treeObj.expandAll(true); 
	selectRootNode(); 
}
 
function selectByNewName(node,newName) { 
	$("#classid").val(node.name);
	var parentTreeNode = node.getParentNode();
	var treeNodeName = newName;
	var string = "";
	while (true) {
		if (parentTreeNode == null) {
			string = string + '<span class=" fontOrange2">' + treeNodeName + '</span>';
			$("#category").html(string);
			return false;
		}
		string = parentTreeNode.name + ">" + string;
		parentTreeNode = parentTreeNode.getParentNode();
	}
	string = string + '<span class=" fontOrange2">' + treeNodeName + '</span>';
	$("#category").html(string);
}

function tree_adddode() {  
	// 选中节点
	layer.prompt({
		  title: '输入车型编号',
		  maxlength:2,
		  formType: 0 //prompt风格，支持0-2
		}, function(pass){ 
			var treeObj = $.fn.zTree.getZTreeObj("treeDemo"); 
			$.post(top.urlpath + "/gxgljson_addtreenode.action?classid=" + pass, {}, function(result, resultState) {
				if (result.state == "0") {
					top.showSuccessMsg("添加成功");   
					layer.closeAll();  
					var treeNode = treeObj.addNodes(null, {
						id : pass,
						pId :0,
						name : "新建车型"
					});
					treeObj.editName(treeNode[0]);
				}else
				{
					top.showErrorMsg("添加失败");   
				} 
			});  
			
		}); 
	
}
function tree_deletenode() {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0];
	if (nodes.length == 0) {
		top.showErrorMsg("请先选择一个节点");
		return;
	}
	if (treeNode.name == '车型') {
		top.showErrorMsg('车型为系统初始化分类，不允许编辑、删除。');
		return;
	}
	
	layer.confirm("确认删除车型"+treeNode.name+"吗？", {
		  btn: ['确定','取消'] //按钮
		}, function(){
			var param = {};
			param["classid"] = treeNode.id; 
			$.post(top.urlpath + "/gxgljson_deletetreenode.action", param, function(result, resultState) {
				if (result.state == "0") {
					top.showSuccessMsg("删除成功");  
					layer.closeAll(); 
					zTree.removeNode(treeNode, true);
				}else
				{
					top.showErrorMsg("删除失败");   
				} 
			});
	}); 
	 
}
// 编辑后
function beforeRename(treeId, treeNode, newName) {
	// treeId即为所操作的树的id
	if (newName.length == 0) {
		top.showErrorMsg("节点名称不能为空.");
		return false;
	}
	var param = {};
	param["parmcode"] = treeNode.id; 
	param["parmname"] = newName; 
	newName=newName.trim();
	if(treeNode.name==newName)
	{
		top.showSuccessMsg("名称没有变化");
		return true;
	}
	if (newName.length == 0) {
		top.showErrorMsg("车型名称不能为空.");
		return false;
	} 
	var lowerName=newName.toLowerCase();
	if("null"==lowerName)
	{
		return false;
	}
	var flag=true; 
		$.ajax({ 
			async:false,
			contentType: "application/x-www-form-urlencoded",
			url : "gxgljson_updatetreenode.action",  
		    data:param,
		    type:'post',    
		    cache:false,    
		    dataType:'json',    
		    success:function(result) {
		    	if (result.state == "0") {
					top.showSuccessMsg("修改车型成功"); 
					flag=true;
				} else {
					if(result.state == "1")
					{
						top.showErrorMsg("您所输入的车型名称重复，请您重新输入");
 						var zTree = $.fn.zTree.getZTreeObj("treeDemo");
 						var nodes = zTree.getSelectedNodes();
 						var treeNode = nodes[0];
 						zTree.editName(treeNode);
						flag=false;
					}
					else
					{
						zTree = $.fn.zTree.getZTreeObj("treeDemo");
						nodes = zTree.getSelectedNodes();
						treeNode = nodes[0];
						var tId = treeNode.tId + "_span";
						treeNode["name"] = tempclassname;
						$("#" + tId).html(tempclassname);
						top.showErrorMsg("更改失败");
						flag=true;
					}
				}
		    },    
		    error : function() {    
		    },
		    beforeSend:function(){
			}
		}); 
	selectByNewName(treeNode,newName);
	return flag;
}
 
// 生成uuid
function newGuid() {
	var guid = "";
	for (var i = 1; i <= 32; i++) {
		var n = Math.floor(Math.random() * 16.0).toString(16);
		guid += n;
		if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
			guid += "-";
	}
	return guid;
}

// 修改节点
function tree_updatenode() {
	 
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getSelectedNodes(), treeNode = nodes[0];
	if (nodes.length == 0) {
		top.showErrorMsg("请先选择一个节点");
		return;
	}
	 
	zTree.editName(treeNode); 
	tempclassname = treeNode.name;
}

// 双击重命名
function onDblClick(event, treeId, treeNode) { 
	var zTree = $.fn.zTree.getZTreeObj(treeId);
	zTree.editName(treeNode);
	zTree.selectNode(treeNode); 
	tempclassname = treeNode.name;
}
 
function getChildren(ids, treeNode) {
	// 非空判断
	if (treeNode.id != null) {
		ids.push(treeNode.id);
	}
	if (treeNode.isParent) {
		for ( var obj in treeNode.children) {
			getChildren(ids, treeNode.children[obj]);
		}
	}
	return ids;
}

function selectRootNode() {
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var nodes = treeObj.getNodes();
	if(nodes !=null)
    {
	  var treeNode =nodes[0];
	  treeObj.selectNode(treeNode);
	  var parentTreeNode = treeNode.getParentNode();
	  var treeNodeName = treeNode.name;
	  var string = "";
	  while (true) {
			if (parentTreeNode == null) {
				break;
			}
			string = parentTreeNode.name + ">" + string;
			parentTreeNode = parentTreeNode.getParentNode();
		}
		treeNodeName=treeNodeName.replace(/</g,"&lt");
		treeNodeName=treeNodeName.replace(/>/g,"&gt");
		string = string + '<span class=" fontOrange2">' + treeNodeName + '</span>';
		$("#category").html(string);
		$("#classid").val(treeNode.name);  
	}
}
function selectNodeById() {
	var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	var node = treeObj.getNodeByParam("name", $("#classid").val(), null);
	if (node == null) {
		var nodes = treeObj.getNodes();
		if(nodes !=null)
	    {
			node =nodes[0];
	    }
	}
	treeObj.selectNode(node);
	if (node != null) {
		var parentTreeNode = node.getParentNode();
		var treeNodeName = node.name;
		treeNodeName=treeNodeName.replace(/</g,"&lt");
		treeNodeName=treeNodeName.replace(/>/g,"&gt");
		var string = "";
		while (true) {
			if (parentTreeNode == null) {
				string = string + '<span class=" fontOrange2">' + treeNodeName + '</span>';
				$("#category").html(string);
				return false;
			}
			string = parentTreeNode.name + ">" + string;
			parentTreeNode = parentTreeNode.getParentNode();
		}
		string = string + '<span class=" fontOrange2">' + treeNodeName + '</span>';
		$("#category").html(string);
		$("#classid").val(node.name);  
	}
}

function expandAll()
{ 
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.expandAll(true);
}
function collapseAll()
{
	var zTree = $.fn.zTree.getZTreeObj("treeDemo");
	zTree.expandAll(false);
}
String.prototype.trim = function() {  
    return (this.replace(/^\s+|\s+$/g,""));  
};  